using System.Collections;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Xml.XPath;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using PS.WebParts.Rollup;
using PS.WebParts.Rollup.Core;

namespace PS.WebParts
{
    public class SPDataSourceViewWrap : ATabularDataSourceView
    {
        //private SPDataSourceView spView;
        private SPDataSource owner;
        public SPDataSourceViewWrap(HttpContext context, IDataSource owner, string viewName) 
            : base(context, owner, viewName)
        {
            this.owner = (SPDataSource)owner;
            //spView = new SPDataSourceView(owner, viewName);
        }

        public override DataTable GetData()
        {
            SPDataSourceView view = owner.GetView();
            IEnumerable enumerable = view.Select(DataSourceSelectArguments.Empty);

            //IEnumerable enumerable = spView.Select(new DataSourceSelectArguments());
            return (DataTable) enumerable;
        }

        public override DataTable GetSchema()
        {
            throw new System.NotImplementedException();
        }
    }

    public class SPDataSourceWrap : ATabularDataSource, IJoinable
    {
        private SPDataSource dataSource = new SPDataSource();
        
        public SPDataSourceWrap()
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb("/r2"))
                {
                    dataSource.DataSourceMode = SPDataSourceMode.CrossList;
                    dataSource.Scope = SPViewScope.Default;
                    SPList list =  web.Lists["DataList"];
                    dataSource.List = list;
                }
            }
        }

        protected override DataSourceView GetView(string viewName)
        {
            return new SPDataSourceViewWrap(this.Context, dataSource, viewName);
            //return dataSource.GetViewOpen(viewName);
        }

        protected override ATabularDataSourceView CreateViewInstance()
        {
            throw new System.NotImplementedException();
        }

        #region Implementation of IJoinable

        public FieldInfoCollection GetFields()
        {
            FieldInfoCollection res = new FieldInfoCollection();
            string query = "<SelectCommand>" + dataSource.SelectCommand + "</SelectCommand>";
            using (StringReader sr = new StringReader(query))
            {
                XPathDocument xDoc = new XPathDocument(sr);
                XPathNavigator xNav = xDoc.CreateNavigator();
                XPathNodeIterator xIter = xNav.Select("//ViewFields/FieldRef[@Name]");
                foreach (XPathNavigator item in xIter)
                {
                    string fieldName = item.GetAttribute("Item", string.Empty);
                    FieldInfo fi = new FieldInfo(fieldName);
                    fi.Type = SPFieldType.Text;
                    res.Add(fi);
                }

            }
            return res;
        }

        public string Query
        {
            get { return dataSource.SelectCommand; }
            set { dataSource.SelectCommand = value; }
        }

        #endregion
    }

    public class OpenSPDataSource : SPDataSource
    {
        public DataSourceView GetViewOpen(string viewName)
        {
            return base.GetView(viewName);
        }

    }
}